"""Captcha (Completely Automated Public Turing test to tell Computers and
Humans Apart) module for django

Author: Martin Winkler, June 2007
License: BSD
"""

from os import path, sep
from urllib import basejoin
from datetime import datetime
from django.conf import settings
from django.utils.translation import gettext
import random

CAPTCHA = {
    # ImageCaptcha and DynImageCaptcha
    'fgcolor': '#000000', # default:  '#000000' (color for characters and lines)
    'bgcolor': '#ffffff', # default:  '#ffffff' (color for background)
    'captchaconf_dir': None, # default:  None  (uses the directory of the captcha module)
    'minmaxvpos': (8, 15), # default:  (8, 15) (vertical position of characters)
    'minmaxrotations': (-30,31), # default:  (-30,31) (rotate characters)
    'minmaxheight': (30,45), # default:  (30,45) (font size)
    'minmaxkerning': (-2,1), # default:  (-2,1) (space between characters)
    'alphabet': "abdeghkmnqrt2346789AEFGHKMNRT", # characters used for captchas
    'num_lines': 1, # default: 1
    'line_width': 3, # default: 3
    'eraser': True, # use an eraser-line for each individual character
    'eraser_width': 1, # width of the eraser-line
    'imagesize': (200,60), # image dimensions in pixels
    'iterations': 1, # default 1 (change to a high value (200 is a good choice)
                     # for trying if your new font-specific settings fit the imagesize)
    # ImageCaptcha only
    'captchas_dir': None, # default:  None (uses MEDIA_ROOT/captchas)
    'auto_cleanup': True, # default:  True (delete all captchas older than 20 minutes)
    'captchabase_url': None, # default:  None (uses MEDIA_URL/captchas)
    # DynImageCaptcha only
    'dyncaptchabase_url': '/captcha', # base url for dynamic images.
                     # REMEMBER to put a line like this in your urls.py
                     # (r'^captcha/(?P<code>.+?)/$', 'django.contrib.captcha.dynimagecaptcha.render_image'),
    }


class captchasettings(object):
    def __init__(self, options=None, seednum=None):
        self.csettings = CAPTCHA.copy()
        if options is None:
            options = {}
        try:
            for k in settings.CAPTCHA.keys():
                self.csettings[k] = settings.CAPTCHA[k]
        except:
            pass
        for k in options.keys():
            self.csettings[k] = options[k]
        self.csettings['captchas_dir'] = self.csettings.get('captchas_dir') or \
                path.join(settings.MEDIA_ROOT, 'captchas')
        self.csettings['captchabase_url'] = self.csettings.get('captchabase_url') or \
                basejoin(settings.MEDIA_URL, "captchas")
        if not self.csettings['captchaconf_dir']:
            self.csettings['captchaconf_dir'] = __file__[:__file__.rfind(sep)]
        self.csettings['timestamp'] = datetime.now()
        self.csettings['R'] = random.Random(seednum)
        self.generate_solution(seednum)

    def generate_solution(self, seednum=None):
        # generate solution
        self.csettings['solution'] = ''
        for i in range(self.csettings['R'].choice((5,6))):
            self.csettings['solution'] += self.csettings['R'].choice(self.csettings['alphabet'])

    def __getitem__(self, what):
        return self.csettings[what]

    def __setitem__(self, key, value):
        self.csettings[key] = value

    def get(self, key, fallback=None):
        try:
            return self.csettings[key]
        except:
            return fallback

errormsg = gettext(u'You did not enter the correct code. Please try again.')

from dynimagecaptcha import render_image

